package com.zhongke.cotrun.controller;


import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.Facilitator;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.FacilitatorService;
import com.zhongke.cotrun.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/facilitator")
public class FacilitatorController {

    @Autowired
    FacilitatorService facilitatorService;
    @Autowired
    UserInfoService userInfoService;

    /**
     * 服务商修改和添加接口
     *
     * @param facilitator
     * @param session
     * @return
     * @throws MyException
     */
    /**
     * 服务商修改和添加接口
     *
     * @param facilitator
     * @param session
     * @return
     * @throws MyException
     */
    @PostMapping("/facilitatorAffirm")
    public APIResponse facilitatorAffirm(Facilitator facilitator, HttpSession session) throws MyException {

        //判断用户是否登录和类型是否正确
        Integer linkUserId = facilitator.getLinkUser();

        Facilitator f = facilitatorService.selectFacilitatorByUserId(linkUserId);


        if (f == null) {
            if (facilitatorService.insertFacilitator(facilitator)) {
                return APIResponse.returnSuccess();
            }
            return APIResponse.returnFail(CodeMsg.ADD_ERROR);
        } else {
            facilitator.setFacilitatorId(f.getFacilitatorId());
            if (facilitatorService.updateFacilitator(facilitator)) {
                return APIResponse.returnSuccess();
            }
            return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);
        }

    }


    /**
     * 查看自己的服务商认证信息
     *
     * @param linkUserId 用户登录的id号
     * @return
     */
    @GetMapping("/selectMyFacilitatorByLinkId")
    public APIResponse selectMyFacilitatorByLinkId(Integer linkUserId) {
        return APIResponse.returnSuccess(facilitatorService.selectFacilitatorByUserId(linkUserId));
    }
}
